from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
from WebAppInterfaceAutomationForAllCollectionFiles.ZhengzhouWebAutomationProject.WebAutomationEncapsulation.PublicToolsCategory.My_mysql import My_mysql


class UserRegisterPage:

    def __init__(self, driver):
        self.driver = driver


    def register_page(self, username, password, repassword):
        WebDriverWait(self.driver, 10, 0.5).until(EC.title_is("度小满理财-登录[内测版]"))
        self.driver.find_element(By.PARTIAL_LINK_TEXT, '去注册').click()
        WebDriverWait(self.driver, 10, 0.5).until(EC.title_is("个人理财系统注册界面"))
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, '#username').send_keys(username)
        self.driver.find_element(By.CSS_SELECTOR, '#password').send_keys(password)
        self.driver.find_element(By.CSS_SELECTOR, '#repassword').send_keys(repassword)
        self.driver.find_element(By.TAG_NAME, "button").click()


    def register_success_get_massage(self):
        return self.driver.title


    def register_massage_sql_select(self, username):
        db = My_mysql()
        db.get_mysql(host='localhost', user='root', password='root', db='finance', port=3306)
        sql = "select * from user where username = %s"
        param = [username]
        return db.select_sql(sql, param)


    def register_massage_sql_delete(self, username):
        db = My_mysql()
        db.get_mysql(host='localhost', user='root', password='root', db='finance', port=3306)
        sql = "delete from user where username = %s"
        param = [username]
        db.update_sql(sql, param)


    def register_username_error_get_massage(self):
        return self.driver.find_element(By.XPATH, '/html/body/div/div/div/form/div[1]/span[1]').text


    def register_password_error_get_massage(self):
        return self.driver.find_element(By.XPATH, '/html/body/div/div/div/form/div[2]/span[1]').text


    def register_repassword_error_get_massage(self):
        return self.driver.find_element(By.XPATH, '/html/body/div/div/div/form/div[3]/span[1]').text


    def register_page_password_error(self, username, password, repassword):

        WebDriverWait(self.driver, 10, 0.5).until(EC.title_is("度小满理财-登录[内测版]"))
        self.driver.find_element(By.PARTIAL_LINK_TEXT, '去注册').click()
        WebDriverWait(self.driver, 10, 0.5).until(EC.title_is("个人理财系统注册界面"))
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, '#username').send_keys(username)

        if password == None:
            self.driver.find_element(By.CSS_SELECTOR, '#password').send_keys('')
        else:
            self.driver.find_element(By.CSS_SELECTOR, '#password').send_keys(password)
        if repassword == None:
            self.driver.find_element(By.CSS_SELECTOR, '#repassword').send_keys('')
        else:
            self.driver.find_element(By.CSS_SELECTOR, '#repassword').send_keys(repassword)

        self.driver.find_element(By.TAG_NAME, "button").click()
